package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //查询用户所有地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(addressBookService.list(queryWrapper));
    }

    //新增地址
    @PostMapping
    public R<String> addab(@RequestBody AddressBook addressBook) {
        //关联用户
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }

    //设置默认地址
    @PutMapping("/default")
    public R<String> autoab(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
// set is_default = 0
        qw.set(AddressBook::getIsDefault, 0);
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());// where user_id =?
        addressBookService.update(qw);

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault, 1);
        wrapper.eq(AddressBook::getId, addressBook.getId());
        addressBookService.update(wrapper);
        return R.success("设置成功");
    }



    //删除地址
    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);

        return R.success("删除成功");

    }

    //修改地址
    @PutMapping
    public R<String> updateab(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    //查询单个
    @GetMapping("/{id}")
    public R<AddressBook> getone(@PathVariable Long id){

        AddressBook byId = addressBookService.getById(id);

        return R.success(byId);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }


}
